{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&emsp;&emsp;**写代码让雪花飘起来**。用python  turtle math random 等函数设置参数，做出雪花飘落的场景\n",
    "依力克亚尔·麦麦提 \n",
    "\n",
    "和其他编程语言相比，Python 只用了很少的新语法和语义就能做出来许多有创意的作品。而且语法简单易懂，初学者很快就能掌握其中的技巧。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先是引入要用到的函数，将其打在最前面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle as tu\n",
    "import random as ra\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们定义画布大小，画出来的颜色，还有画笔的状态等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "tu.setup(1.0, 1.0)     \n",
    "tu.screensize(1.0, 1.0)    #设置画布大小\n",
    "tu.bgcolor('black')  #设置画布颜色\n",
    "t = tu.Pen()         \n",
    "t.ht()               #隐藏画笔\n",
    "colors = ['white']   #雪花的颜色列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们定义我们屏幕要出现的物体，比如雪花。将雪花的形状用循环语句画出来，设置初始坐标，大小，速度，颜色等参数。确定雪花飘的轨迹和飘雪的数量，频率，出场和退场。结束语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle as tu\n",
    "import random as ra\n",
    "import math\n",
    "tu.setup(1.0, 1.0)     \n",
    "tu.screensize(1.0, 1.0)    #设置画布大小\n",
    "tu.bgcolor('black')  #设置画布颜色\n",
    "t = tu.Pen()         \n",
    "t.ht()               #隐藏画笔\n",
    "colors = ['white']   #雪花的颜色列表\n",
    "class Snow():    #雪花类\n",
    "    def __init__(self):\n",
    "        self.r = 6                       #雪花的半径\n",
    "        self.x = ra.randint(-1000,1000)   #雪花的横坐标\n",
    "        self.y = ra.randint(-500,500)     #雪花的纵坐标\n",
    "        self.f = ra.uniform(-3.14,3.14)   #雪花左右移动呈正弦函数\n",
    "        self.speed = ra.randint(5,10)     #雪花移动速度\n",
    "        self.color = ra.choice(colors)    #雪花的颜色\n",
    "        self.outline = 5                 #雪花的大小\n",
    "    def snow(self):                #画每个雪花\n",
    "        x=self.r                   #雪花的半径\n",
    "        t.pensize(self.outline)    #雪花的大小\n",
    "        t.penup()                  #提笔\n",
    "        t.goto(self.x,self.y)      #随机位置\n",
    "        t.pendown()                #落笔\n",
    "        t.color(self.color)        #雪花颜色\n",
    "        for i in range(6):        #循环画六个雪花瓣\n",
    "            t.forward(x*5)\n",
    "            t.backward(x*2)\n",
    "            t.left(60)\n",
    "            t.forward(x*2)\n",
    "            t.backward(x*2)\n",
    "            t.right(120)\n",
    "            t.forward(x*2)\n",
    "            t.backward(x*2)\n",
    "            t.left(60)\n",
    "            t.backward(x*3)\n",
    "            t.right(60)    \n",
    "    def move(self):                    #雪花移动函数\n",
    "        if self.y >= -500:            #当雪花还在画布中时\n",
    "            self.y -= self.speed     #设置上下移动速度\n",
    "            self.x -= self.speed * math.sin(self.f)    #设置左右移动速度\n",
    "            self.f -= 0.1            #可以理解成标志，改变左右移动的方向\n",
    "        else:                        #当雪花漂出了画布时，重新生成一个雪花\n",
    "            self.r = 6        \n",
    "            self.x = ra.randint(-1000,1000)\n",
    "            self.y = 500\n",
    "            self.f = ra.uniform(-3.14,3.14)\n",
    "            self.speed = ra.randint(5,10)\n",
    "            self.color = ra.choice(colors)\n",
    "            self.outline = 5 \n",
    "Snows = []            #用列表保存所有雪花\n",
    "for i in range(100):     \n",
    "    Snows.append(Snow())      \n",
    "while True:           #开始绘制\n",
    "    tu.tracer(0)       \n",
    "    t.clear()\n",
    "    for i in range(90):    #90个漂浮的雪花\n",
    "        Snows[i].move()\n",
    "        Snows[i].snow()\n",
    "    tu.update()\n",
    "tu.mainloop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以通过改变参数，能改变雪花的颜色，大小，速度。比如彩色雪花。{将颜色列表中的颜色增加自己想要的颜色，就能制作五颜六色的雪花了！！！}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import turtle as tu\n",
    "import random as ra\n",
    "import math\n",
    "tu.setup(1.0, 1.0)     \n",
    "tu.screensize(1.0, 1.0)    #设置画布大小\n",
    "tu.bgcolor('black')  #设置画布颜色\n",
    "t = tu.Pen()         \n",
    "t.ht()               #隐藏画笔\n",
    "colors = ['white','yellow','red','green','orange','blue']   #雪花的颜色列表\n",
    "class Snow():    #雪花类\n",
    "    def __init__(self):\n",
    "        self.r = 6                       #雪花的半径\n",
    "        self.x = ra.randint(-1000,1000)   #雪花的横坐标\n",
    "        self.y = ra.randint(-500,500)     #雪花的纵坐标\n",
    "        self.f = ra.uniform(-3.14,3.14)   #雪花左右移动呈正弦函数\n",
    "        self.speed = ra.randint(5,10)     #雪花移动速度\n",
    "        self.color = ra.choice(colors)    #雪花的颜色\n",
    "        self.outline = 5                 #雪花的大小\n",
    "    def snow(self):                #画每个雪花\n",
    "        x=self.r                   #雪花的半径\n",
    "        t.pensize(self.outline)    #雪花的大小\n",
    "        t.penup()                  #提笔\n",
    "        t.goto(self.x,self.y)      #随机位置\n",
    "        t.pendown()                #落笔\n",
    "        t.color(self.color)        #雪花颜色\n",
    "        for i in range(6):        #循环画六个雪花瓣\n",
    "            t.forward(x*5)\n",
    "            t.backward(x*2)\n",
    "            t.left(60)\n",
    "            t.forward(x*2)\n",
    "            t.backward(x*2)\n",
    "            t.right(120)\n",
    "            t.forward(x*2)\n",
    "            t.backward(x*2)\n",
    "            t.left(60)\n",
    "            t.backward(x*3)\n",
    "            t.right(60)    \n",
    "    def move(self):                    #雪花移动函数\n",
    "        if self.y >= -500:            #当雪花还在画布中时\n",
    "            self.y -= self.speed     #设置上下移动速度\n",
    "            self.x -= self.speed * math.sin(self.f)    #设置左右移动速度\n",
    "            self.f -= 0.1            #可以理解成标志，改变左右移动的方向\n",
    "        else:                        #当雪花漂出了画布时，重新生成一个雪花\n",
    "            self.r = 6        \n",
    "            self.x = ra.randint(-1000,1000)\n",
    "            self.y = 500\n",
    "            self.f = ra.uniform(-3.14,3.14)\n",
    "            self.speed = ra.randint(5,10)\n",
    "            self.color = ra.choice(colors)\n",
    "            self.outline = 5 \n",
    "Snows = []            #用列表保存所有雪花\n",
    "for i in range(100):     \n",
    "    Snows.append(Snow())      \n",
    "while True:           #开始绘制\n",
    "    tu.tracer(0)       \n",
    "    t.clear()\n",
    "    for i in range(90):    #90个漂浮的雪花\n",
    "        Snows[i].move()\n",
    "        Snows[i].snow()\n",
    "    tu.update()\n",
    "tu.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
